<?php

namespace app\common\model\Depot\Back;

use app\common\model\Business\Business;
use app\common\model\Express;
use think\Model;
use traits\model\SoftDelete;

class Back extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'depot_back';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'status_text',
        'province_text',
        'city_text',
        'district_text',
    ];
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '-1' => __('Status -1')];
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function business()
    {
        return $this->belongsTo(Business::class,'busid','id',[],'left')->setEagerlyType(0);
    }
    public function express()
    {
        return $this->belongsTo(Express::class,'expressid','id',[],'left')->setEagerlyType(0);
    }
    public function getProvinceTextAttr($value,$data)
    {
        return $data['province']?model('region')->where('code',$data['province'])->find()['name']:'';
    }
    public function getCityTextAttr($value,$data)
    {
        return $data['city']?model('region')->where('code',$data['city'])->find()['name']:'';
    }
    public function getDistrictTextAttr($value,$data)
    {
        return $data['district']?model('region')->where('code',$data['district'])->find()['name']:'';
    }
}
